package com.topscomm.pm.quartz;

import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.pm.pojo.PmProjectEntity;
import com.topscomm.pm.service.impl.PmProjectService;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.quartz.TapJob;
import org.quartz.JobExecutionContext;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: wushenghong
 * @date: 2020/7/24
 * @description: 检测超期进行中项目，延期完成的回退项目状态为进行中
 */
public class PmProjectOverdueBackJob extends TapJob {

    @Override
    public void run(JobExecutionContext jobExecutionContext) {
        String today = cn.hutool.core.date.DateUtil.formatDate(new Date());
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" enabled=1 ");
        sql.appendSingle("and planendtime >={0} and projectstate = '{1}'", SQLAdapter.toDateLong(today + " 00:00:00"), "4");
        PmProjectService pmProjectService = (PmProjectService) SpringUtil.getBean("pmProjectService");
        List<PmProjectEntity> pmProjectEntityList = pmProjectService.queryByWhere(sql.toString());
        List<BasicEntity> pmProjectEntities = new ArrayList<BasicEntity>();
        if (pmProjectEntityList.size() > 0) {
            for (PmProjectEntity pmProjectEntity : pmProjectEntityList) {
                pmProjectEntity.setProjectstate(2);//将项目进度状态设置为进行中
                pmProjectEntities.add(pmProjectEntity);
            }
            List<BasicEntity> entities = new ArrayList<BasicEntity>();
            //数据批量更新
            for (int i = 0; i < pmProjectEntities.size(); i++) {
                entities.add(pmProjectEntities.get(i));
                if (i > 0 && i % 500 == 0) {
                    pmProjectService.updateBatch(entities);
                    entities.clear();
                }
            }
            if (entities.size() > 0) {
                pmProjectService.updateBatch(entities);
            }
        }
    }
}
